<template>
  <el-row type="flex" class="row-bg" justify="center">
    <el-col :span="6">
      <div class="grid-content bg-purple">
        <h1>欢迎来到XXX管理系统</h1>
        <el-image :src="require('@/assets/MarkerHub.jpg')" style="height: 180px; width: 180px;"></el-image>
        <p>公众号</p>
        <p>扫描二维码</p>

      </div>
    </el-col>
    <el-col :span="1">
      <div class="grid-content bg-purple-light">
        <el-divider direction="vertical"></el-divider>
      </div>
    </el-col>
    <el-col :span="6">
      <div class="grid-content bg-purple">
        <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="loginForm.password"></el-input>
          </el-form-item >
          <el-form-item label="验证码" prop="code" class="code">
            <el-col :span="11">
              <el-input v-model="loginForm.code" ></el-input>
            </el-col>
            <el-col :span="11">
              <el-image :src="captchaImg" class="captchaImg" @click="getCaptcha"></el-image>
            </el-col>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('loginForm')">立即创建</el-button>
            <el-button @click="resetForm('loginForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-col>
  </el-row>

</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        code: '',
        token:''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
          {min: 5, max: 5, message: '长度 5 个字符', trigger: 'blur'}
        ],
      },
      captchaImg: ''
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('/login',this.loginForm).then(res=>{
                const jwt=res.headers['authorization']
                this.$store.commit('SET_TOKEN',jwt)
            this.$router.push('/index')
          })
         // alert('submit!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    getCaptcha() {
      this.$axios.get('/captcha').then(res => {

        console.log("/captcha")
        console.log(res)

        this.loginForm.token = res.data.data.token
        this.captchaImg = res.data.data.captchaImg
        this.loginForm.code = ''
      })
    }
  },
  created() {
    this.getCaptcha()
  }
}
</script>

<style scoped>
.el-row {
  background-color: #fafafa;
  height: 100%;
  display: flex;
  align-items: center;
  text-align: center;
  justify-content: center;
}
.el-divider{
  height: 200px;
}

.el-form-item__content{}
</style>